<?php #//AJAX PHP PENNY AUCTION v.1.1

#///////////////////////////////////////////////////////////
#//  COPYRIGHT 2009 Aaron Helmlinger ALL RIGHTS RESERVED  //
#///////////////////////////////////////////////////////////

include "./includes/config.inc.php";

#///////////////////////////////////////////////////////
#// GET ITEM ID #
#///////////////////////////////////////////////////////

 
                   $cron = 0;
    while($cron < 10) {
 
 $auction_id = $_GET['id'];
 
 //echo $auction_id;

#///////////////////////////////////////////////////////
#// GET SETTINGS FROM DATABASE
#///////////////////////////////////////////////////////

$query_settings = "select * from PHPAUCTIONXL_unique_settings where id='1'";
                             $query_settings2 =  mysql_query ($query_settings);
                                $activated = mysql_result($query_settings2,0,"activated");
                                $frequency = mysql_result($query_settings2,0,"frequency");
                                $minimum = mysql_result($query_settings2,0,"minimum");
                                $maximum = mysql_result($query_settings2,0,"maximum");
                                $openingtime = mysql_result($query_settings2,0,"openingtime");
                                $closingtime = mysql_result($query_settings2,0,"closingtime");
                                $firstbid = mysql_result($query_settings2,0,"firstbid");
#///////////////////////////////////////////////////////
#// GET CURRENT DATE & TIME
#///////////////////////////////////////////////////////

  $TIME = mktime(date("H")+$SETTINGS['timecorrection'],date("i"),date("s"),date("m"), date("d"),date("Y"));

$date_time_now = date("YmdHis",$TIME);
$NOW = $date_time_now;

 #///////////////////////////////////////////////////////
#// CHECK TO SEE IF OPENED OR CLOSED
#///////////////////////////////////////////////////////

 $checkhour = substr ($NOW, 8, 2); // Hour
    // echo  $checkhour;
 //echo $checkhour;
 //echo $openingtime;
 //echo $closingtime;
 
 


if ( $checkhour >= $openingtime && $checkhour <= $closingtime ) {

   $activated2 = 1;
  
}


 if ( $openingtime == 0 && $closingtime == 0 ) {
  
  $activated2 = 1;

  }

#///////////////////////////////////////////////////////
#// CHECK TO SEE IF THIS FEATURE IS ACTIVATED
#///////////////////////////////////////////////////////

if ($activated == 1 && $activated2 == 1){

             

#///////////////////////////////////////////////////////
#// CHECK IF ACTIVATED BY ITEM PAGE USING AUCTION ID OR INDEX PAGE
#///////////////////////////////////////////////////////


if (!$auction_id) {

#///////////////////////////////////////////////////////
#// GET ACTIVE AUCTIONS FROM DATABASE 
#///////////////////////////////////////////////////////

$query = "select * from PHPAUCTIONXL_auctions
         WHERE closed='0' AND
         suspended='0' AND ";
         $query .= "starts<=".$NOW."
         order by ends LIMIT ".$SETTINGS['endingsoonnumber'];
$result = mysql_query($query);



$num_auction = mysql_num_rows($result);
    //echo $num_auction;
#///////////////////////////////////////////////////////
#// RANDOMLY SELECT ITEM TO BID ON 
#///////////////////////////////////////////////////////  

 srand ((double) microtime( )*1000000);
$randomly_selected_auction_item = rand(0,($num_auction-1));
   $i = $randomly_selected_auction_item;
   
   if ( ($num_auction - 1) == 0 ) { $i=0; }
   
   
#///////////////////////////////////////////////////////
#// SET VARIABLE FOR ITEM TO BID ON 
#///////////////////////////////////////////////////////
      
      $title	= mysql_result($result,$i,"title");
	$id	= mysql_result($result,$i,"id");
	 $starts 	= mysql_result($result,$i,"starts");
  $ends 	= mysql_result($result,$i,"ends");
  $increment = mysql_result($result,$i,"increment");
  $high_bidder 	= mysql_result($result,$i,"current_high_bidder");
	$current_bid 	= mysql_result($result,$i,"current_bid");
   $closed 	= mysql_result($result,$i,"closed");
	  $unique1 	= mysql_result($result,$i,"unique1");
     $unique2 	= mysql_result($result,$i,"unique2");
       $reserve 	= mysql_result($result,$i,"reserve_price");
                      $item_extend_activated = mysql_result($result,$i,"ae_activation");
  $item_extend_increment = mysql_result($result,$i,"ae_increment");

      // echo $title;
  /*   echo $id;
      echo $starts;
       echo $ends;
       echo $increment;
        echo $high_bidder;
 */    
#///////////////////////////////////////////////////////
#// GET LAST BID TIME FOR THE AUCTION 
#///////////////////////////////////////////////////////  
 /*
$query_last_bid_time = "select bidwhen from PHPAUCTIONXL_bids where auction='$id' DESC";
                             $query_last_bid_time2 =  mysql_query ($query_last_bid_time);
                                $last_bid_time = mysql_result($query_last_bid_time2,0,"bidwhen");
 */
 
 }
 

if ($auction_id) {

#///////////////////////////////////////////////////////
#// GET ACTIVE AUCTIONS FROM DATABASE 
#///////////////////////////////////////////////////////
   

$query = "select * from PHPAUCTIONXL_auctions
         WHERE id=$auction_id";
$result = mysql_query($query);
$num_auction = mysql_num_rows($result);
 
 $i = 0;
#///////////////////////////////////////////////////////
#// SET VARIABLE FOR ITEM TO BID ON 
#///////////////////////////////////////////////////////
  
      $title	= mysql_result($result,$i,"title");
	$id	= mysql_result($result,$i,"id");
	 $starts 	= mysql_result($result,$i,"starts");
  $ends 	= mysql_result($result,$i,"ends");
  $increment = mysql_result($result,$i,"increment");
  $high_bidder 	= mysql_result($result,$i,"current_high_bidder");
	$current_bid 	= mysql_result($result,$i,"current_bid");
   $closed 	= mysql_result($result,$i,"closed");
	  $unique1 	= mysql_result($result,$i,"unique1");
     $unique2 	= mysql_result($result,$i,"unique2");
            $reserve 	= mysql_result($result,$i,"reserve_price");
                      $item_extend_activated = mysql_result($result,$i,"ae_activation");
  $item_extend_increment = mysql_result($result,$i,"ae_increment");   
 } 
     


 
   
#/////////////////////////////////////////////////////////////////////
#// CALCULATE SECONDS LEFT UNTIL AUCTION ENDS
#/////////////////////////////////////////////////////////////////////

$time_ends_seconds = substr ($ends, 0, 4); // Year
$time_ends_seconds .= "-";
$time_ends_seconds .= substr ($ends, 4, 2); // Month
$time_ends_seconds .= "-";
$time_ends_seconds .= substr ($ends, 6, 2); // Day
$time_ends_seconds .= " ";
$time_ends_seconds .= substr ($ends, 8, 2); // Hour
$time_ends_seconds .= ":";
$time_ends_seconds .= substr ($ends, 10, 2); // Minute
$time_ends_seconds .= ":";
$time_ends_seconds .= substr ($ends, 12, 2); // Second

$time_now_seconds = date("Y-m-d H:i:s",$TIME);

  
 
// convert to unix timestamps
$firstTime=strtotime($time_now_seconds);
$lastTime=strtotime($time_ends_seconds);

// perform subtraction to get the difference (in seconds) between times
$timeDiff=$lastTime-$firstTime;



$time_end_diffrence_in_seconds = $timeDiff;

$seconds_until_auction_ends = $time_end_diffrence_in_seconds;
$minutes_until_auction_ends = ($time_end_diffrence_in_seconds / 60);
$hours_until_auction_ends = ($time_end_diffrence_in_seconds / 60) / 60;




#/////////////////////////////////////////////////////////////////////
#// CALCULATE SECONDS AFTER AUCTION STARTED
#/////////////////////////////////////////////////////////////////////

$time_starts_seconds = substr ($starts, 0, 4); // Year
$time_starts_seconds .= "-";
$time_starts_seconds .= substr ($starts, 4, 2); // Month
$time_starts_seconds .= "-";
$time_starts_seconds .= substr ($starts, 6, 2); // Day
$time_starts_seconds .= " ";
$time_starts_seconds .= substr ($starts, 8, 2); // Hour
$time_starts_seconds .= ":";
$time_starts_seconds .= substr ($starts, 10, 2); // Minute
$time_starts_seconds .= ":";
$time_starts_seconds .= substr ($starts, 12, 2); // Second

 
// convert to unix timestamps
$firstTime1=strtotime($time_now_seconds);
$lastTime1=strtotime($time_starts_seconds);

// perform subtraction to get the difference (in seconds) between times

$timeDiff1=$firstTime1-$lastTime1;

$time_starts_diffrence_in_seconds = $timeDiff1;

$seconds_until_auction_starts = $time_starts_diffrence_in_seconds;
$minutes_until_auction_starts = ($time_starts_diffrence_in_seconds / 60);
$hours_until_auction_starts = ($time_starts_diffrence_in_seconds / 60) / 60;

//$time_start_diffrence_in_seconds = timeDiff ($time_starts_seconds,$time_now_seconds);  
 
$hours_since_going_live = $hours_until_auction_starts;

                
                
                          
//echo $hours_since_going_live;    
     
#///////////////////////////////////////////////////////
#// RANDOMLY DECIDE TO BID ON AN ITEM AT ALL
#///////////////////////////////////////////////////////

$activate_bid_range = 0;
$auction_id_index = $id;
$unique1_users =  $unique1;
$unique2_users =  $unique2;

if ( $seconds_until_auction_ends > 180 && $unique1_users != 1000 ) {

/// IF AN AUCTION IS CLOSER TO ENDING, THERE WILL BE MORE ACTIVITY FOR THAT AUCTION


  

if ( $hours_until_auction_ends <= 3 ) {

      $activate_bid_range = 3;

}
 
if ( $hours_until_auction_ends <= 2 ) {

      $activate_bid_range = 4;

}

if ( $hours_until_auction_ends <= 1 ) {

      $activate_bid_range = 5;

}

   if ( $activate_bid_range < 3 ) {
        
        $peak_hour_start = 0;
        $peak_hour_end = 8;

if ( $checkhour >= $peak_hour_start && $checkhour <= $peak_hour_end ) {


 $activate_bid_range = 1;
 
 }
 
 
} 


   if ( $activate_bid_range < 3 ) {
        
        $peak_hour_start = 9;
        $peak_hour_end = 13;

if ( $checkhour >= $peak_hour_start && $checkhour <= $peak_hour_end ) {


 $activate_bid_range = 2;
 
 }
 
 
} 


   if ( $activate_bid_range < 3 ) {
        
        $peak_hour_start = 14;
        $peak_hour_end = 17;

if ( $checkhour >= $peak_hour_start && $checkhour <= $peak_hour_end ) {


 $activate_bid_range = 3;
 
 }
 
 
} 



  if ( $activate_bid_range < 3 ) {
        
        $peak_hour_start = 18;
        $peak_hour_end = 23;

if ( $checkhour >= $peak_hour_start && $checkhour <= $peak_hour_end ) {


 $activate_bid_range = 4;
 
 }
 
 
} 

      
     #/// BESIDES THE SETTING IN THE ADMIN PANEL YOU CAN
     #/// REDUCE THE FREQUENCY THAT UNIQUE BIDDERS BID
     #/// BY CHANGING THE "$freaquency_multiplier" BELOW
     
    $frequency_multiplier = 5; 
         
     #/// ENTER A NUMBER FROM 1 to 10 ABOVE.
     #/// 1 = More bids   10 = fewer bids
  
  
  
  if ( $activate_bid_range < 3 ) {
        
        $peak_hour_start = 0;
        $peak_hour_end = 8;

if ( $checkhour >= $peak_hour_start && $checkhour <= $peak_hour_end ) {


 $frequency_multiplier = 5;
 
 }
 
 
} 
  
  
       
       $frequency_multiplier2 = $frequency_multiplier * $frequency;   
    srand ((double) microtime( )*1000000); 
    
    
    
    
$randomly_activate_bid = rand(1, $frequency_multiplier2 );

              //echo $randomly_activate_bid;


if ( $randomly_activate_bid > 0 && $randomly_activate_bid <= $activate_bid_range ) {
      // echo $randomly_activate_bid;
     //   echo "<BR><BR>";
     $going_to_bid = 1;
     // echo $going_to_bid;
     // echo "<BR><BR>";
    //  echo $activate_bid_range;
     // echo "<BR><BR>";

/*      echo "<BR> seconds until end";
echo $seconds_until_auction_ends;  
echo "<BR> minutes until end";
echo $minutes_until_auction_ends;  
echo "<BR> hours until end";
echo $hours_until_auction_ends;   */
#/////////////////////////////////////////////////////////////////////
#// RANDOMLY SELECT HOUR TO START BIDDING FOR ITEM
#/////////////////////////////////////////////////////////////////////

$first_bid_hour_start = $firstbid;


$first_bid_hour_end = $firstbid;


 srand ((double) microtime( )*1000000);
$randomly_selected_first_bid_hour = rand($first_bid_hour_start, $first_bid_hour_end);
       $start_hour_okay = 0;
       
if ( $firstbid < $hours_since_going_live && $seconds_until_auction_ends > 180 ) {

$start_hour_okay = 1;
    
}


if($start_hour_okay == 1 ) {

 // working echo   $randomly_selected_first_bid_hour;
#/////////////////////////////////////////////////////////////////////
#// DETERMINE HOW MANY UNIQUE USERS ARE TO BID OR NONE AT ALL
#/////////////////////////////////////////////////////////////////////



           

if ( $unique1_users != 1000 && $unique1_users == 0  )  {

 
$unique_users_start = $minimum;
   //echo $minimum;
  //echo $unique_users_start;    

  $unique_users_end = $maximum;
  
      // echo $maximum;
    // echo $unique_users_end;   

   
if ( $unique_users_start == 0 || $unique_users_start >= $unique_users_end ) $unique_users_start = 1;


                     

                        
if ( $unique_users_end == 0 || $unique_users_end <= $unique_users_start ) $unique_users_end = $unique_users_start + 1;

 srand ((double) microtime( )*1000000);
$randomly_selected_unique_users = rand( $unique_users_start , $unique_users_end );

#/////////////////////////////////////////////////////////////////////
#// SET NUMBER OF UNIQUE USERS IN AUCTION DATABASE FOR ITEM SELECTED
#/////////////////////////////////////////////////////////////////////
 
 $query_unique1_users ="UPDATE PHPAUCTIONXL_auctions SET unique1='$randomly_selected_unique_users' WHERE id='$auction_id_index'";

mysql_query($query_unique1_users);

}

#/////////////////////////////////////////////////////////////////////
#// IF UNIQUE1 USER IS IS ZERO QUERY DATABASE TO GET STARTING VARIABLE
#/////////////////////////////////////////////////////////////////////


if ( $unique1_users == 0 ) {


$query_unique1_users2 = "select unique1 from PHPAUCTIONXL_auctions where id=$auction_id_index";
                             $query_unique1_users3 =  mysql_query ($query_unique1_users2);
                                $unique1_users = mysql_result($query_unique1_users3,0,"unique1");

} 


#/////////////////////////////////////////////////////////////////////
#// CHECK TO SEE IF UNIQUE2 IS LESS THAN UNIQUE1
#/////////////////////////////////////////////////////////////////////


if ( $unique2_users < $unique1_users && $unique2_users > 2 ) {

$unique2_less_than_unique1 = 1;

}

else {

$unique2_less_than_unique1 = 0;

}

#/////////////////////////////////////////////////////////////////////
#// DECIDE TO USE NEW UNIQUE USER OR OLD UNIQUE USER
#/////////////////////////////////////////////////////////////////////


     srand ((double) microtime( )*1000000);
$new_unique1 = rand(1,3);

if ( $new_unique1 == 2 ) {

$select_new_unique1 = 1;

}

else {

$select_new_unique1 = 0;

}

#/////////////////////////////////////////////////////////////////////
#// SELECT NEW UNIQUE USER FROM "USERS" DATABASE MORE THAN 2 UNIQUE2
#/////////////////////////////////////////////////////////////////////

if( $unique2_less_than_unique1 == 1 && $select_new_unique1 == 1 ) {

$query = "select nick,id from PHPAUCTIONXL_users
         WHERE accounttype='unique1'";
$result = mysql_query($query);
$num_auction = mysql_num_rows($result);


     srand ((double) microtime( )*1000000);
$random_user = rand(0,$num_auction);

 $i = $random_user;


  $username 	= mysql_result($result,$i,"nick");
  $userid 	= mysql_result($result,$i,"id");


$unique2_users++;


}

#/////////////////////////////////////////////////////////////////////
#// SELECT NEW UNIQUE USER FROM "USERS" DATABASE IF LESS THAN 2 UNIQUE2
#/////////////////////////////////////////////////////////////////////


if( $unique2_users < 3  ) {

$query = "select nick,id from PHPAUCTIONXL_users
         WHERE accounttype='unique1'";
$result = mysql_query($query);
$num_auction = mysql_num_rows($result);

     srand ((double) microtime( )*1000000);
$random_user = rand(0,$num_auction);

 $i = $random_user;


  $username 	= mysql_result($result,$i,"nick");
  $userid 	= mysql_result($result,$i,"id");


$unique2_users++;
}


#/////////////////////////////////////////////////////////////////////
#// SELECT EXISTING UNIQUE USER FROM "BIDS" DATABASE B/C SELECT NEW UNIQUE1 SAYS NO
#/////////////////////////////////////////////////////////////////////

if( $unique2_less_than_unique1 == 1 && $select_new_unique1 == 0 ) {

$query = "select current_high_bidder,bidder from PHPAUCTIONXL_bids2
         WHERE accounttype='unique1' AND auction='$auction_id_index'";
$result = mysql_query($query);
$num_auction = mysql_num_rows($result);


      srand ((double) microtime( )*1000000);
$random_user = rand(0,$num_auction);

 $i = $random_user;


  $username 	= mysql_result($result,$i,"current_high_bidder");
  $userid 	= mysql_result($result,$i,"bidder");


}

#/////////////////////////////////////////////////////////////////////
#// SELECT EXISTING UNIQUE USER FROM "BIDS" DATABASE B/C UNIQUE1 & 2 EQUAL
#/////////////////////////////////////////////////////////////////////

if( $unique2_users == $unique1_users ) {


    



$query = "select current_high_bidder,bidder from PHPAUCTIONXL_bids2
         WHERE accounttype='unique1' AND auction='$auction_id_index'";
$result = mysql_query($query);
$num_auction = mysql_num_rows($result);


      srand ((double) microtime( )*1000000);
$random_user = rand(0,$num_auction);

 $i = $random_user;


  $username 	= mysql_result($result,$i,"current_high_bidder");
  $userid 	= mysql_result($result,$i,"bidder");


}

   
   
   

#/////////////////////////////////////////////////////////////////////
#// ADD NEW UNIQUE1 WITH 4 HOURS TO GO
#/////////////////////////////////////////////////////////////////////

 if ( $hours_until_auction_ends < 5 && $unique1_users == $unique2_users ) {
 
 
 srand ((double) microtime( )*1000000);
$randomly_activate_bid2 = rand(1,$frequency);


if ( $randomly_activate_bid2 == 1 ) {

     
      
$query = "select nick,id from PHPAUCTIONXL_users
         WHERE accounttype='unique1'";
$result = mysql_query($query);
$num_auction = mysql_num_rows($result);

     srand ((double) microtime( )*1000000);
$random_user = rand(0,$num_auction);

 $i = $random_user;


  $username 	= mysql_result($result,$i,"nick");
  $userid 	= mysql_result($result,$i,"id");

$unique2_users++;
 
}
}
 
#/////////////////////////////////////////////////////////////////////
#// ADD NEW UNIQUE1 WITH 3 HOURS TO GO
#/////////////////////////////////////////////////////////////////////

 if ( $hours_until_auction_ends < 4 && ($unique1_users + 1) == $unique2_users ) {
 
      
 
 srand ((double) microtime( )*1000000);
$randomly_activate_bid2 = rand(1,$frequency);


if ( $randomly_activate_bid2 == 1 ) {
     
      
$query = "select nick,id from PHPAUCTIONXL_users
         WHERE accounttype='unique1'";
$result = mysql_query($query);
$num_auction = mysql_num_rows($result);

     srand ((double) microtime( )*1000000);
$random_user = rand(0,$num_auction);

 $i = $random_user;


  $username 	= mysql_result($result,$i,"nick");
  $userid 	= mysql_result($result,$i,"id");

$unique2_users++;
 
} 
} 
 

#/////////////////////////////////////////////////////////////////////
#// ADD NEW UNIQUE1 WITH 2 HOURS TO GO
#/////////////////////////////////////////////////////////////////////

 if ( $hours_until_auction_ends < 3 && ($unique1_users + 2) == $unique2_users ) {
 
 
 srand ((double) microtime( )*1000000);
$randomly_activate_bid2 = rand(1,$frequency);


if ( $randomly_activate_bid2 == 1 ) {

     
$query = "select nick,id from PHPAUCTIONXL_users
         WHERE accounttype='unique1'";
$result = mysql_query($query);
$num_auction = mysql_num_rows($result);

     srand ((double) microtime( )*1000000);
$random_user = rand(0,$num_auction);

 $i = $random_user;


  $username 	= mysql_result($result,$i,"nick");
  $userid 	= mysql_result($result,$i,"id");

$unique2_users++;
 
} 
  }
 
#/////////////////////////////////////////////////////////////////////
#// ADD NEW UNIQUE1 WITH 1 HOURS TO GO
#/////////////////////////////////////////////////////////////////////

 if ( $hours_until_auction_ends < 2 && ($unique1_users + 3) == $unique2_users ) {
 
    
 srand ((double) microtime( )*1000000);
$randomly_activate_bid2 = rand(1,$frequency);


if ( $randomly_activate_bid2 == 1 ) {

  
$query = "select nick,id from PHPAUCTIONXL_users
         WHERE accounttype='unique1'";
$result = mysql_query($query);
$num_auction = mysql_num_rows($result);

     srand ((double) microtime( )*1000000);
$random_user = rand(0,$num_auction);

 $i = $random_user;


  $username 	= mysql_result($result,$i,"nick");
  $userid 	= mysql_result($result,$i,"id");

$unique2_users++;
 
} 
 }
 

#/////////////////////////////////////////////////////////////////////
#// SELECT RANDOM BIDDER FROM ACTIVE BIDDERS DURING FINAL HOUR
#///////////////////////////////////////////////////////////////////// 
 
 
 
 if ( $minutes_until_auction_ends < 350 && $seconds_until_auction_ends > 180 ) {
 
 
 srand ((double) microtime( )*1000000);
$randomly_activate_bid2 = rand(1,$frequency);


if ( $randomly_activate_bid2 == 1 ) {

 
$query = "select current_high_bidder,bidder from PHPAUCTIONXL_bids2
         WHERE accounttype='unique1' AND auction='$auction_id_index'";
$result = mysql_query($query);
$num_auction = mysql_num_rows($result);


      srand ((double) microtime( )*1000000);
$random_user = rand(0,$num_auction);

 $i = $random_user;


  $username 	= mysql_result($result,$i,"current_high_bidder");
  $userid 	= mysql_result($result,$i,"bidder");

 
} 

}
 
 



   
 



  //  echo "<BR><BR>";
   // echo $username;
   //  echo "<BR><BR>";

} // Start Bid
} // Randomly Activate Bid
} // NOT WITHIN 180 SECONDS OF ENDING

              // $seconds_until_auction_ends = 10;
               
   if ( $seconds_until_auction_ends < 180 ) {



$query = "select current_high_bidder,bidder from PHPAUCTIONXL_bids2
         WHERE accounttype='unique1' AND auction='$auction_id_index'";
$result = mysql_query($query);
$num_auction = mysql_num_rows($result);


      srand ((double) microtime( )*1000000);
$random_user = rand(1,11);
             // echo $random_user;
 $i = $random_user;


  $username 	= mysql_result($result,$i,"current_high_bidder");
  $userid 	= mysql_result($result,$i,"bidder");


      $going_to_bid = 1;

      if( $username == $high_bidder )
      {
      
          $going_to_bid = 0;
      
      }


}
 



 #/////////////////////////////////////////////////////////////////////
#// DEACTIVATED SCRIPT FOR ITEM
#/////////////////////////////////////////////////////////////////////

if ( $unique1 == 1000 )  {

$going_to_bid = 0;

}
     
#/////////////////////////////////////////////////////////////////////
#// DEACTIVATED SCRIPT FOR ITEM IF CLOSED                                	
#/////////////////////////////////////////////////////////////////////

if ( $closed == 1 )  {

$going_to_bid = 0;

} 

 
 
 #/////////////////////////////////////////////////////////////////////
#// DEACTIVATED SCRIPT FOR ITEM IF TIME ELAPSED                                	
#/////////////////////////////////////////////////////////////////////

if ( $ends < $NOW )  {

$going_to_bid = 0;

} 
                 
                 
                 
              if ( $firstbid > $hours_since_going_live ) {

$going_to_bid = 0;
    
}        

#/////////////////////////////////////////////////////////////////////
#// CHECK IF SELECTED UNIQUE1 IS CURRENT HIGH BIDDER
#/////////////////////////////////////////////////////////////////////


if ( $going_to_bid == 1 && $username ) {


//echo "BOT BID";


if ( $seconds_until_auction_ends > 180 ) {

$username_check = $high_bidder;


 if ( $username == $username_check ) {


             
$username 	= mysql_result($result,0,"current_high_bidder");
 $userid 	= mysql_result($result,0,"bidder");

}

  if ( $username == $username_check ) {



$username 	= mysql_result($result,1,"current_high_bidder");
    $userid 	= mysql_result($result,1,"bidder");

}

if ( $username == $username_check ) {



$username 	= mysql_result($result,2,"current_high_bidder");
    $userid 	= mysql_result($result,2,"bidder");

}

}

#/////////////////////////////////////////////////////////////////////
#// BID ON ITEM
#/////////////////////////////////////////////////////////////////////


$increment = $increment;

$current_bid = $current_bid;

$high_bid = $increment + $current_bid;

//echo $high_bid;

$query1 = "INSERT INTO PHPAUCTIONXL_bids VALUES";

$query1 .= "(NULL";
$query1 .= ",";
$query1 .= $auction_id_index;
$query1 .= ",";
$query1 .= $userid;
$query1 .= ",";
$query1 .= $high_bid;
$query1 .= ",";
$query1 .= $NOW;
$query1 .= ",";
$query1 .= "1";
$query1 .= ",";
$query1 .= "'";
$query1 .= $username;
$query1 .= "'";
$query1 .= ",";
$query1 .= "'";
$query1 .= unique1;
$query1 .= "'";
$query1 .= ")";
     
    // echo $query1;
    // echo "<BR>";       
if (!mysql_query($query1)) {
     $going_to_bid = 0;

}

    $query12 = "INSERT INTO PHPAUCTIONXL_bids2 VALUES";

$query12 .= "(NULL";
$query12 .= ",";
$query12 .= $auction_id_index;
$query12 .= ",";
$query12 .= $userid;
$query12 .= ",";
$query12 .= $high_bid;
$query12 .= ",";
$query12 .= $NOW;
$query12 .= ",";
$query12 .= "1";
$query12 .= ",";
$query12 .= "'";
$query12 .= $username;
$query12 .= "'";
$query12 .= ",";
$query12 .= "'";
$query12 .= unique1;
$query12 .= "'";
$query12 .= ")";
     
    // echo $query12;
    // echo "<BR>";       
if (!mysql_query($query12)) {
     

}



if ( $going_to_bid == 1 ) {
       $query555 = "SELECT counter as counter from PHPAUCTIONXL_auccounter where auction_id=".intval($id);
$rcount=@mysql_query($query555);
if(!$rcount) {
    MySQLError($query);
    exit;
}
if(mysql_num_rows($rcount)==0) {
    mysql_query("INSERT INTO PHPAUCTIONXL_auccounter VALUES (".intval($id).",1)");
    $TPL_nr=1;
} else {

 
      srand ((double) microtime( )*1000000);
$randomnumber = rand(1,3);  
        
if ( $randomnumber == 1 ) {        
    $TPL_nr=mysql_result($rcount,0,"counter");
    mysql_query("UPDATE PHPAUCTIONXL_auccounter set counter='".intval($TPL_nr+1)."' where auction_id=".intval($id));
        }
        
        if ( $randomnumber == 2 ) {        
    $TPL_nr=mysql_result($rcount,0,"counter");
    mysql_query("UPDATE PHPAUCTIONXL_auccounter set counter='".intval($TPL_nr+2)."' where auction_id=".intval($id));
        }
        
              if ( $randomnumber == 3 ) {        
    $TPL_nr=mysql_result($rcount,0,"counter");
    mysql_query("UPDATE PHPAUCTIONXL_auccounter set counter='".intval($TPL_nr+1)."' where auction_id=".intval($id));
        }
}





   
 
$query2="UPDATE PHPAUCTIONXL_auctions SET current_high_bidder='$username', current_bid='$high_bid', unique2='$unique2_users' WHERE id='$auction_id_index'";
   // echo $query2; 
mysql_query($query2);

$query233="UPDATE PHPAUCTIONXL_auctions2 SET current_high_bidder='$username', current_bid='$high_bid' WHERE id='$auction_id_index'";
   // echo $query233;
mysql_query($query233);


$auctionends = $ends;   
   
       $query435 = mysql_fetch_array(mysql_query("SELECT bid_credits_reqd,ends,ae_activation,ae_increment,time_increment,hard_reset FROM PHPAUCTIONXL_auctions WHERE id=$auction_id_index "));
  

 $auctionends = $query435['ends'];
  $item_extend_activated = $query435['ae_activation'];
  $item_extend_increment = $query435['ae_increment'];
  $bid_credits_reqd1 = $query435['bid_credits_reqd'];
     $hard_reset_check = $query435['hard_reset'];
  
  
  if ( $hard_reset_check == 0 ) {
  
  if ( $item_extend_activated == 0) {  
    /**
        * NOTE: AUCTION AUTOEXTENSION
        */
        $EXTSETTINGS = mysql_fetch_array(mysql_query("SELECT * FROM PHPAUCTIONXL_auctionextension"));
        if ($EXTSETTINGS['status'] == 'enabled') {
          $__END = mktime(substr($auctionends, 8, 2), substr($auctionends, 10, 2), substr($auctionends, 12, 2), substr($auctionends, 4, 2), substr($auctionends, 6, 2), substr($auctionends, 0, 4));
            
            $TIME_EXTEND = $EXTSETTINGS['timebefore'] + 10;
            
            if (($__END - $TIME) <= $TIME_EXTEND) {
                $auctionends = date("YmdHis", mktime(substr($auctionends, 8, 2), substr($auctionends, 10, 2), substr($auctionends, 12, 2) + ($EXTSETTINGS['extend'] + 3), substr($auctionends, 4, 2), substr($auctionends, 6, 2), substr($auctionends, 0, 4)));
           
            }

            $query69 = "UPDATE PHPAUCTIONXL_auctions set ends='$auctionends' WHERE id=$auction_id_index";
                mysql_query($query69);
                
                            $query692 = "UPDATE PHPAUCTIONXL_auctions2 set ends='$auctionends' WHERE id=$auction_id_index";
                mysql_query($query692);
            
            
             if (($__END - $TIME) <= 0 ) {
            
                    $closed = "1";
                    
                          }
                          
                          
        }
            }

         if ( $item_extend_activated > 0 ) {  
    /**
        * NOTE: AUCTION AUTOEXTENSION
        */
           if ( 1 == 1 ) {
          $__END = mktime(substr($auctionends, 8, 2), substr($auctionends, 10, 2), substr($auctionends, 12, 2), substr($auctionends, 4, 2), substr($auctionends, 6, 2), substr($auctionends, 0, 4));
            
                $TIME_EXTEND = $item_extend_activated + 10;
            if (($__END - $TIME) <= $TIME_EXTEND ) {
                $auctionends = date("YmdHis", mktime(substr($auctionends, 8, 2), substr($auctionends, 10, 2), substr($auctionends, 12, 2) + ($item_extend_increment + 3), substr($auctionends, 4, 2), substr($auctionends, 6, 2), substr($auctionends, 0, 4)));
           
            }

       $query69 = "UPDATE PHPAUCTIONXL_auctions set ends='$auctionends' WHERE id=$auction_id_index";
                mysql_query($query69);
                
                    $query693 = "UPDATE PHPAUCTIONXL_auctions2 set ends='$auctionends' WHERE id=$auction_id_index";
                mysql_query($query693);
            
             if (($__END - $TIME) <= 0 ) {
            
                    $closed = "1";
                    
                          }
                          
                          
        }
            }

                }
                
                
                
                
                
            if ( $hard_reset_check == 1 ) {
            
            
            
                 if ( $item_extend_activated == 0) {  
    /**
        * NOTE: AUCTION AUTOEXTENSION
        */
        $EXTSETTINGS = mysql_fetch_array(mysql_query("SELECT * FROM PHPAUCTIONXL_auctionextension"));
        if ($EXTSETTINGS['status'] == 'enabled') {
          $__END = mktime(substr($auctionends, 8, 2), substr($auctionends, 10, 2), substr($auctionends, 12, 2), substr($auctionends, 4, 2), substr($auctionends, 6, 2), substr($auctionends, 0, 4));
            
            $TIME_EXTEND = $EXTSETTINGS['timebefore'];
                     
                         $timeremaining22 = $__END - $TIME;
              $item_extend_increment1 = $EXTSETTINGS['extend'] - $timeremaining22;
              
            if (($__END - $TIME) <= $TIME_EXTEND + 8 ) {
                $auctionends = date("YmdHis", mktime(substr($auctionends, 8, 2), substr($auctionends, 10, 2), substr($auctionends, 12, 2) + ($item_extend_increment1 + 8), substr($auctionends, 4, 2), substr($auctionends, 6, 2), substr($auctionends, 0, 4)));
           
            }

            $query69 = "UPDATE PHPAUCTIONXL_auctions set ends='$auctionends' WHERE id=$auction_id_index";
                mysql_query($query69);
                
                            $query692 = "UPDATE PHPAUCTIONXL_auctions2 set ends='$auctionends' WHERE id=$auction_id_index";
                mysql_query($query692);
            
             if (($__END - $TIME) <= 0 ) {
            
                    $closed = "1";
                    
                          }
                          
                          
        }
            }

         if ( $item_extend_activated > 0 ) {  
    /**
        * NOTE: AUCTION AUTOEXTENSION
        */
           if ( 1 == 1 ) {
          $__END = mktime(substr($auctionends, 8, 2), substr($auctionends, 10, 2), substr($auctionends, 12, 2), substr($auctionends, 4, 2), substr($auctionends, 6, 2), substr($auctionends, 0, 4));
            
                $TIME_EXTEND = $item_extend_activated;
                          $timeremaining22 = $__END - $TIME;
              $item_extend_increment1 = $item_extend_increment - $timeremaining22;
            if (($__END - $TIME) <= $TIME_EXTEND + 8 ) {
                $auctionends = date("YmdHis", mktime(substr($auctionends, 8, 2), substr($auctionends, 10, 2), substr($auctionends, 12, 2) + ($item_extend_increment1 + 8), substr($auctionends, 4, 2), substr($auctionends, 6, 2), substr($auctionends, 0, 4)));
           
            }

       $query69 = "UPDATE PHPAUCTIONXL_auctions set ends='$auctionends' WHERE id=$auction_id_index";
                mysql_query($query69);
                
                          $query693 = "UPDATE PHPAUCTIONXL_auctions2 set ends='$auctionends' WHERE id=$auction_id_index";
                mysql_query($query693);
                
             if (($__END - $TIME) <= 0 ) {
            
                    $closed = "1";
                    
                          }
                          
                          
        }
            }


   
                        
            
            
            
            }    
       










   


 }    
} // End Bid








} // SCRIPT ACTIVATED
  $cron++;
   if ( $cron == 9 )
  
  {
  mysql_close($linktodatabase);
  exit;
    }
              srand ((double) microtime( )*1000000); 
     
$sleepseconds = rand(4,10 );
      
              sleep($sleepseconds);

        }
    
   ?>